module DistributedTestRunner
  
  class LoggedTestRunnerServer

    def initialize(server)
      @server = server
    end

    def succeeded?
      @server.succeeded?
    end

    def reports
      @server.reports
    end

    def run
      logger.info {"start to run server..."}
      @server.run
    end

    def add_client(client)
      logger.info {"adding client: #{client.to_yaml}"}
      @server.clients << client
    end

    def update(signature, report)
      logger.debug {"updating report: #{report.to_yaml}"}
      @server.update(signature, report)
      logger.info {"#{@server.%} completed"}
    end
  end

  class DrbTestRunnerClientServer

    def initialize(my_uri, server_uri)
      @my_uri = my_uri
      @server_uri = server_uri
    end

    def queue
      @queue ||= []
    end

    def idle?
      queue.empty?
    end

    def run(runner_name, test_files, signature)
      queue << [runner_name, test_files, signature]
    end

    def start
      DRb.start_service(@my_uri, self)
      logger.info {"Client server started, waiting for commands..."}

      loop do
        unless queue.empty?
          logger.info {"Working..."}
          logger.debug {queue.first.to_yaml}

          dtrClient = Client.new(drbServer, 'dtr client')
          dtrClient.run *queue.first.dup
          queue.pop
          logger.info {"Sleeping..."}
        end
        sleep(5)
      end
    end

    private
    def drbServer
      @drbServer ||= DRbObject.new_with_uri(@server_uri)
    end
  end
end



